package com.ruoyi.pad.service.impl;



import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.pad.domain.KyBaseMotor;
import com.ruoyi.pad.mapper.KyBaseMotorMapper;
import com.ruoyi.pad.service.IKyBaseMotorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 电机库Service业务层处理
 *
 *
 * @date 2023-02-08
 */
@Service
public class KyBaseMotorServiceImpl implements IKyBaseMotorService {

    @Autowired(required = false)
    private KyBaseMotorMapper kyBaseMotorMapper;

    /**
     * 查询电机库
     *
     * @param motorId 电机库主键
     * @return 电机库
     */
    @Override
    public KyBaseMotor selectKyBaseMotorByMotorId(Long motorId) {
        return kyBaseMotorMapper.selectKyBaseMotorByMotorId(motorId);
    }

    /**
     * 查询电机库列表
     *
     * @param kyBaseMotor 电机库
     * @return 电机库
     */
    @Override
    public List<KyBaseMotor> selectKyBaseMotorList(KyBaseMotor kyBaseMotor) {
        return kyBaseMotorMapper.selectKyBaseMotorList(kyBaseMotor);
    }

    /**
     * 新增电机库
     *
     * @param kyBaseMotor 电机库
     * @return 结果
     */
    @Override
    public int insertKyBaseMotor(KyBaseMotor kyBaseMotor) {
        kyBaseMotor.setCreateTime(DateUtils.getNowDate());
        return kyBaseMotorMapper.insertKyBaseMotor(kyBaseMotor);
    }

    /**
     * 修改电机库
     *
     * @param kyBaseMotor 电机库
     * @return 结果
     */
    @Override
    public int updateKyBaseMotor(KyBaseMotor kyBaseMotor) {
        kyBaseMotor.setUpdateTime(DateUtils.getNowDate());
        return kyBaseMotorMapper.updateKyBaseMotor(kyBaseMotor);
    }

    /**
     * 批量删除电机库
     *
     * @param motorIds 需要删除的电机库主键
     * @return 结果
     */
    @Override
    public int deleteKyBaseMotorByMotorIds(Long[] motorIds) {
        return kyBaseMotorMapper.deleteKyBaseMotorByMotorIds(motorIds);
    }

    /**
     * 删除电机库信息
     *
     * @param motorId 电机库主键
     * @return 结果
     */
    @Override
    public int deleteKyBaseMotorByMotorId(Long motorId) {
        return kyBaseMotorMapper.deleteKyBaseMotorByMotorId(motorId);
    }


    /**
     * 查询自定义电机库
     *
     * @param deptId 部门ID
     * @param userId 用户ID
     * @return
     */
    @Override
    public List<KyBaseMotor> selectCustomList(Long deptId, Long userId) {
        return kyBaseMotorMapper.selectCustomList(deptId, userId);
    }

    /**
     * 批量插入电机库信息
     * @param motorVoList
     * @return
     */
    @Override
    public int insertKyBaseMotorBatch(List<KyBaseMotor> motorVoList) {
        return kyBaseMotorMapper.insertKyBaseMotorBatch(motorVoList);
    }

    /**
     * txt导入
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertToDbFromTxT() {
        try {
            File file = ResourceUtils.getFile("classpath:motorlib.txt");
            InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file), "UTF-8");
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String Result = "";
            Object var5 = null;
            Long startTime = System.currentTimeMillis();
            List<KyBaseMotor> motorBeans = new ArrayList();
            while ((line = bufReader.readLine()) != null) {
                String[] mlibStr = line.split("\t");
                if (mlibStr.length < 9) {
                } else {
                    if (mlibStr.length > 0) {
                        KyBaseMotor mlib = new KyBaseMotor();
                        mlib.setBelongFlag("0");
                        mlib.setDjxl(new String(mlibStr[0].getBytes("UTF-8"), "UTF-8"));
                        mlib.setDjxh(mlibStr[1]);
                        mlib.setJsh(mlibStr[2]);
                        mlib.setEdgl(mlibStr[3]);
                        mlib.setEddl(mlibStr[4]);
                        mlib.setEdxl(mlibStr[5]);
                        mlib.setKzdl(mlibStr[6]);
                        mlib.setKzgl(mlibStr[7]);
                        mlib.setEddy(mlibStr[8]);
                        mlib.setEdglysh("0.84");
                        mlib.setWgjjdl("0.08");
                        motorBeans.add(mlib);
                    }
                }
            }

            Long endTime = System.currentTimeMillis();
            System.out.println("转换用时 ================>>> " + (endTime - startTime));
            int okCount = kyBaseMotorMapper.insertKyBaseMotorBatch(motorBeans);
            System.out.println("转换用时 ================>>> 总共插入" + okCount + " 条数据 ，用时 " + (System.currentTimeMillis() - endTime));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }


    /**
     * 系统电机库查询
     * @return
     */
    @Override
    public List<KyBaseMotor> selectKyBaseMotorListOfSys() {
        return kyBaseMotorMapper.selectKyBaseMotorListOfSys();
    }
}
